<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编码" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType1">
        <el-select v-model="queryParams.deviceType1" placeholder="请选择设备类型" clearable>
          <el-option
            v-for="dict in dict.type.device_type1"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类别" prop="deviceType2">
        <el-select v-model="queryParams.deviceType2" placeholder="请选择设备类别" clearable>
          <el-option
            v-for="dict in dict.type.check_type1"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备大类" prop="deviceTyp3">
        <el-select v-model="queryParams.deviceTyp3" placeholder="请选择设备大类" clearable>
          <el-option
            v-for="dict in dict.type.device_typ3"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="deviceModel">
        <el-input
          v-model="queryParams.deviceModel"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:deviceInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:deviceInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:deviceInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:deviceInfo:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-printer"
          size="mini"
          @click="printPi2"
        >批量打印</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="公司名称" align="center" prop="factoryName" />
      <el-table-column label="公司代码" align="center" prop="factoryCode" />
      <el-table-column label="设备编码" align="center" prop="deviceCode" />
      <el-table-column label="设备类型" align="center" prop="deviceType1">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_type1" :value="scope.row.deviceType1"/>
        </template>
      </el-table-column>
      <el-table-column label="设备类别" align="center" prop="deviceType2">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_type1" :value="scope.row.deviceType2"/>
        </template>
      </el-table-column>
      <el-table-column label="设备大类" align="center" prop="deviceTyp3">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_typ3" :value="scope.row.deviceTyp3"/>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="规格型号" align="center" prop="deviceModel" />
      <el-table-column label="设备功率" align="center" prop="devicePower" />
      <el-table-column label="设备序列码" align="center" prop="deviceSeq" />
      <el-table-column label="设备标记" align="center" prop="deviceIsFlag" />
      <el-table-column label="购置时间" align="center" prop="purchaseTime" />
      <el-table-column label="资产负责人" align="center" prop="chargePerson" />
      <el-table-column label="使用寿命" align="center" prop="serviceLife" />
      <el-table-column label="供应商编码" align="center" prop="supplyId" />
      <el-table-column label="供应商名称" align="center" prop="supplyName" />
      <el-table-column label="制造商编码" align="center" prop="makerCode" />
      <el-table-column label="制造商名称" align="center" prop="makerName" />
      <el-table-column label="使用部门" align="center" prop="deptId" />
      <el-table-column label="使用状况" align="center" prop="useStatus" />
      <el-table-column label="设备位置" align="center" prop="devicePosition" />
      <el-table-column label="设备地址" align="center" prop="deviceAddress" />
      <el-table-column label="ip地址" align="center" prop="ipAddress" />
      <el-table-column label="MAC地址" align="center" prop="macAddress" />
      <el-table-column label="操作人员" align="center" prop="operator" />
      <el-table-column label="固定资产编号" align="center" prop="fixedAssetsNo" />
      <el-table-column label="启用时间" align="center" prop="openingTime" />
      <el-table-column label="设备图片地址" align="center" prop="deviceImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.deviceImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="设备二维码地址" align="center" prop="deviceEwm" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.deviceEwm" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="设备保养周期" align="center" prop="maintainCycle1" />
      <el-table-column label="设备维护周期" align="center" prop="maintainCycle2" />
      <el-table-column label="设备程序代码" align="center" prop="programCode" />
      <el-table-column label="设备程序包" align="center" prop="programPackage" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:deviceInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:deviceInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="factoryName">
          <el-input v-model="form.factoryName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="公司代码" prop="factoryCode">
          <el-input v-model="form.factoryCode" placeholder="请输入公司代码" />
        </el-form-item>
        <el-form-item label="设备编码" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType1">
          <el-select v-model="form.deviceType1" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.device_type1"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类别" prop="deviceType2">
          <el-select v-model="form.deviceType2" placeholder="请选择设备类别">
            <el-option
              v-for="dict in dict.type.check_type1"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备大类" prop="deviceTyp3">
          <el-select v-model="form.deviceTyp3" placeholder="请选择设备大类">
            <el-option
              v-for="dict in dict.type.device_typ3"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="deviceModel">
          <el-input v-model="form.deviceModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="设备功率" prop="devicePower">
          <el-input v-model="form.devicePower" placeholder="请输入设备功率" />
        </el-form-item>
        <el-form-item label="设备序列码" prop="deviceSeq">
          <el-input v-model="form.deviceSeq" placeholder="请输入设备序列码" />
        </el-form-item>
        <el-form-item label="设备标记" prop="deviceIsFlag">
          <el-input v-model="form.deviceIsFlag" placeholder="请输入设备标记" />
        </el-form-item>
        <el-form-item label="购置时间" prop="purchaseTime">
          <el-input v-model="form.purchaseTime" placeholder="请输入购置时间" />
        </el-form-item>
        <el-form-item label="资产负责人" prop="chargePerson">
          <el-input v-model="form.chargePerson" placeholder="请输入资产负责人" />
        </el-form-item>
        <el-form-item label="使用寿命" prop="serviceLife">
          <el-input v-model="form.serviceLife" placeholder="请输入使用寿命" />
        </el-form-item>
        <el-form-item label="供应商编码" prop="supplyId">
          <el-input v-model="form.supplyId" placeholder="请输入供应商编码" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplyName">
          <el-input v-model="form.supplyName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="制造商编码" prop="makerCode">
          <el-input v-model="form.makerCode" placeholder="请输入制造商编码" />
        </el-form-item>
        <el-form-item label="制造商名称" prop="makerName">
          <el-input v-model="form.makerName" placeholder="请输入制造商名称" />
        </el-form-item>
        <el-form-item label="使用部门" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入使用部门" />
        </el-form-item>
        <el-form-item label="设备位置" prop="devicePosition">
          <el-input v-model="form.devicePosition" placeholder="请输入设备位置" />
        </el-form-item>
        <el-form-item label="设备地址" prop="deviceAddress">
          <el-input v-model="form.deviceAddress" placeholder="请输入设备地址" />
        </el-form-item>
        <el-form-item label="ip地址" prop="ipAddress">
          <el-input v-model="form.ipAddress" placeholder="请输入ip地址" />
        </el-form-item>
        <el-form-item label="MAC地址" prop="macAddress">
          <el-input v-model="form.macAddress" placeholder="请输入MAC地址" />
        </el-form-item>
        <el-form-item label="操作人员" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入操作人员" />
        </el-form-item>
        <el-form-item label="固定资产编号" prop="fixedAssetsNo">
          <el-input v-model="form.fixedAssetsNo" placeholder="请输入固定资产编号" />
        </el-form-item>
        <el-form-item label="启用时间" prop="openingTime">
          <el-input v-model="form.openingTime" placeholder="请输入启用时间" />
        </el-form-item>
        <el-form-item label="设备图片地址">
          <image-upload v-model="form.deviceImg"/>
        </el-form-item>
        <el-form-item label="设备二维码地址">
          <image-upload v-model="form.deviceEwm"/>
        </el-form-item>
        <el-form-item label="设备保养周期" prop="maintainCycle1">
          <el-input v-model="form.maintainCycle1" placeholder="请输入设备保养周期" />
        </el-form-item>
        <el-form-item label="设备维护周期" prop="maintainCycle2">
          <el-input v-model="form.maintainCycle2" placeholder="请输入设备维护周期" />
        </el-form-item>
        <el-form-item label="设备程序代码" prop="programCode">
          <el-input v-model="form.programCode" placeholder="请输入设备程序代码" />
        </el-form-item>
        <el-form-item label="设备程序包" prop="programPackage">
          <el-input v-model="form.programPackage" placeholder="请输入设备程序包" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <div style="width: 100%;display: none">
      <div style="width: 100%;padding-top:30px;font-size: 20px;" ref="printDan">
        <div v-for="(item,index) in ids" :key="index" style="width: 50%;text-align: center;display: block;float: left;margin-bottom: 20px" >
          <div style="display: inline-block;margin-bottom: 10px" class="qrcode" ref="qrCodeUrl"></div>
<!--          <div>{{item}}</div>-->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { listDeviceInfo, getDeviceInfo, delDeviceInfo, addDeviceInfo, updateDeviceInfo } from "@/api/device/deviceInfo";
import QRCode from "qrcodejs2";
import print from 'print-js';

export default {
  name: "DeviceInfo",
  dicts: ['check_type1', 'device_type1', 'device_typ3'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备列表表格数据
      deviceInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceCode: null,
        deviceType1: null,
        deviceType2: null,
        deviceTyp3: null,
        deviceName: null,
        deviceModel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    printPi2(){
      this.$nextTick(()=>{
        this.ids.forEach((item,index)=>{
          let code = item;
          console.log(code,'aaaaaaaaaaaa')
          this.$refs.qrCodeUrl[index].innerHTML="";
          new QRCode(this.$refs.qrCodeUrl[index], {
            text: code+'abc', //二维码内容
            width: 150,
            height: 150,
            // colorDark: "#333333", //二维码颜色
            // colorLight: "#ffffff", //二维码背景色
          })
        })
        setTimeout(()=>{
          this.priDan()
        },200)
      })
    },
    priDan(){

      const style = '@page { margin:0 } ' +'@media print { #title{ font-size:40px};#title1{ font-size:40px }  ' ;
      printJS({
        printable:this.$refs.printDan,
        type:'html',
        header:'',
        targetStyles:['*'],
        scanStyles: false,
        style:style,
        onPrintDialogClose: () => {
          console.log('弹窗关闭');
        },
        onLoadingEnd:()=>{
          console.log('加载完毕')
        }
      })
    },
    /** 查询设备列表列表 */
    getList() {
      this.loading = true;
      listDeviceInfo(this.queryParams).then(response => {
        this.deviceInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryName: null,
        factoryCode: null,
        deviceCode: null,
        deviceType1: null,
        deviceType2: null,
        deviceTyp3: null,
        deviceName: null,
        deviceModel: null,
        devicePower: null,
        deviceSeq: null,
        deviceIsFlag: null,
        purchaseTime: null,
        chargePerson: null,
        serviceLife: null,
        supplyId: null,
        supplyName: null,
        makerCode: null,
        makerName: null,
        deptId: null,
        useStatus: "0",
        devicePosition: null,
        deviceAddress: null,
        ipAddress: null,
        macAddress: null,
        operator: null,
        fixedAssetsNo: null,
        openingTime: null,
        deviceImg: null,
        deviceEwm: null,
        maintainCycle1: null,
        maintainCycle2: null,
        programCode: null,
        programPackage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDeviceInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeviceInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备列表编号为"' + ids + '"的数据项？').then(function() {
        return delDeviceInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/deviceInfo/export', {
        ...this.queryParams
      }, `deviceInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
